import React from 'react';
import { connect } from 'react-redux';
import { Switch, Route, Redirect, withRouter } from 'react-router-dom';
import Tip from './person/Tip';
import action from '../store/action/index';
import { checkLogin } from '../api/person';

/*IMPORT COMPONENT AND CSS*/
import List from './candidate/List';
import Info from './candidate/Info';
import CreateCandidate from './candidate/Create'
import '../static/css/candidate.less';

class Candidate extends React.Component {
    constructor(props, context) {
        super(props, context);
        //=>STATE
        this.state = {
            isLogin: false
        };
    }

    async componentWillMount(){
        let result = await checkLogin(),
            isLogin = parseFloat(result.code) === 0 ? true : false;
        this.setState({ isLogin });
    }

    componentDidMount(){
        this.setState({ isLogin: this.props.isLogin });
    }

    componentWillReceiveProps() {
        this.setState({ isLogin: this.props.isLogin });
    }

    render() {
        return <section className='candidateBox'>
            <Switch>
                <Route path='/candidate' exact  render={() => {
                    if (this.state.isLogin) {
                        return <List />;
                    }
                    return <Tip />;
                }}  />
                <Route path='/candidate/info' render={() => {
                    if (this.state.isLogin) {
                        return <Info />;
                    }
                    return <Tip />;
                }}  />
                <Route path='/candidate/create' render={() => {
                    if (this.state.isLogin) {
                        return <CreateCandidate />;
                    }
                    return <Tip />;
                }} />
            </Switch>
        </section>;
    }
}

export default withRouter(connect(state => ({...state.person}), action.person)(Candidate));